<template>
  <div class="container">
    <div class="visible">
      <el-timeline>
        <el-timeline-item v-for="(item, index) in history" :key="index" :timestamp="item.time" placement="top">
          <el-card>
            <h4>{{ item.title }}</h4>
            <p>{{ item.content }}</p>
          </el-card>
        </el-timeline-item>
      </el-timeline>
    </div>
    <div class="loading" v-loading="true"></div>
  </div>
</template>

<script>
import moment from "moment";

export default {
  name: "ScrollMore",
  data() {
    return {
      history: []
    }
  },
  methods: {
    loadMore() {
      const timer = setTimeout(() => {
        for (let i = 0; i < 10; i++) {
          this.history.push({
            time: moment().format('YYYY-MM-DD'),
            title: '更新 Github 模板',
            content: `王小虎 提交于 ${moment().format('YYYY-MM-DD HH:mm:ss')}`
          })
        }
        clearTimeout(timer)
      }, 500)
    }
  },
  created() {
    for (let i = 0; i < 10; i++) {
      this.history.push({
        time: moment().format('YYYY-MM-DD'),
        title: '更新 Github 模板',
        content: `王小虎 提交于 ${moment().format('YYYY-MM-DD HH:mm:ss')}`
      })
    }
  },
  mounted() {
    if (window?.IntersectionObserver) {
      const observer = new IntersectionObserver((entries) => {
        entries.forEach(item => {
          if (item.isIntersecting) {
            this.loadMore()
          }
        })
      }, {root: document.querySelector('.container'), threshold: [0]})
      const loading = document.querySelector('.loading')
      observer.observe(loading);
    }
  }
}
</script>

<style lang="less" scoped>
.container {
  width: 400px;
  height: 600px;
  border: 1px solid black;
  overflow: auto;
  padding: 20px;

  .loading {
    height: 100px;
  }
}
</style>
